package cn.edu.ctbu.labtest.webapi;

import cn.edu.ctbu.labtest.core.PageUtils;
import cn.edu.ctbu.labtest.domain.Course;
import cn.edu.ctbu.labtest.domain.Teacher;
import cn.edu.ctbu.labtest.helper.R;
import cn.edu.ctbu.labtest.helper.RUtil;
import cn.edu.ctbu.labtest.service.CourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.util.StringUtils;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/webapi/course")
public class CourseRestController {
    @Autowired
    private CourseService courseService;

    @GetMapping("/findbyname")
    public R<Course> findByName(String name, Integer limit, Integer offset){
        Page<Course> all;
        Pageable pageable = PageRequest.of(offset/limit,limit,
                Sort.by("id").descending());

        if(name==null||name.equals("")){
            all=courseService.findAll(pageable);
        }
        else{
            all=courseService.findByNameLike("%"+name+"%",pageable);
        }
        return RUtil.success(all);
    }

    /**
     * 读取的url:/webapi/course/getByPage
     * 分页读取全部内容
     * @return
     */
    @GetMapping("/getByPage")
    public PageUtils getByPage(@RequestParam(value ="page",defaultValue = "0")Integer page,
                               @RequestParam(value = "size",defaultValue = "10")Integer size,
                               @RequestParam(value = "name",defaultValue = "")String name)
    {

        //按id排倒序
        Sort sort=Sort.by("id").descending();
        Page<Course> coursePage;
        if(StringUtils.isEmpty(name)){
            Pageable pageable=PageRequest.of(page,size,sort);
            coursePage=courseService.findAll(pageable);
        }
        else{
            Course course=new Course();
            course.setName(name);

            Pageable pageable=PageRequest.of(0,10,sort);
            ExampleMatcher matcher=ExampleMatcher.matching()
                    .withMatcher("name",ExampleMatcher.GenericPropertyMatchers.contains());//模糊查询,即%{tname}%
            Example<Course> example=Example.of(course,matcher);
            coursePage=courseService.findAll(example,pageable);
        }
        PageUtils pageUtils=new PageUtils(coursePage.getContent(),coursePage.getTotalElements());
        return  pageUtils;
    }

    /**
     * 读取的url:/webapi/course/clist
     * @return
     */
    @GetMapping("/clist")
    public List<Course> getAll(){
        List<Course> courses=courseService.findAll();
        return courses;
    }

    /**
     * 读取的url:/webapi/course/findByName
     */
    @GetMapping("/findByName")
    public  List<Course> findByName(String name){
        List<Course> all;
        if(StringUtils.isEmpty(name)){
            all=courseService.findAll();
        }
        else{
            all=courseService.findByName(name);
        }
        return all;
    }

    /**
     * 读取的url:/webapi/course/findByNameLike
     */
    @GetMapping("/findByNameLike")
    public  Page<Course> findByNameLike(String name,Pageable pageable){
        Page<Course> all;
        if(StringUtils.isEmpty(name)){
            all=courseService.findAll(pageable);
        }
        else{
            all=courseService.findByNameLike(name,pageable);
        }
        return all;
    }

    /**
     * 读取的url:/webapi/course/get/id
     * @return
     */
    @GetMapping("/get/{id}")
    public Course get(@PathVariable Integer id){
        Course courses=courseService.getById(id);
        //courses.setPassword("");
        return courses;
    }

}
